﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using StoragePeriod.Data;
using System.Text;
using System.Web.Script.Serialization;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;
using StoragePeriod.Model.StorageForecast;
using StoragePeriod.BLL.StorageForecast;

namespace StoragePeriod.Web.Services
{
    public class ReportServices : IAjaxService
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录操作
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作
        T_StorageCalculateResult tStorageCalculateResult = new T_StorageCalculateResult();

        public AjaxResult Excute(HttpContext httpContext)
        {
            string FunType = httpContext.Request["FunType"];
            AjaxResult result = null;
            switch (FunType)
            {
                case "SenseScore":
                    result = GetDefaultResult(httpContext, "SenseScore");
                    break;
                case "VCAssay":
                    result = GetDefaultResult(httpContext, "VCAssay");
                    break;
                case "VAAssay":
                    result = GetDefaultResult(httpContext, "VAAssay");
                    break;
                case "VEAssay":
                    result = GetDefaultResult(httpContext, "VEAssay");
                    break;
                case "AminoAcidData":
                    result = GetDefaultResult(httpContext, "AminoAcidData");
                    break;
                case "DietaryFiberData":
                    result = GetDefaultResult(httpContext, "DietaryFiberData");
                    break;
                case "WaterAssay":
                    result = GetDefaultResult(httpContext, "WaterAssay");
                    break;
                case "ProteinAssay":
                    result = GetDefaultResult(httpContext, "ProteinAssay");
                    break;
                case "MineralsAssay":
                    result = GetDefaultResult(httpContext, "MineralsAssay");
                    break;
                case "Carbohydrates":
                    result = GetDefaultResult(httpContext, "Carbohydrates");
                    break;
                case "CuData":
                    result = GetDefaultResult(httpContext, "CuData");
                    break;
                case "FeData":
                    result = GetDefaultResult(httpContext, "FeData");
                    break;
                case "ZnData":
                    result = GetDefaultResult(httpContext, "ZnData");
                    break;
                case "CaData":
                    result = GetDefaultResult(httpContext, "CaData");
                    break;
                case "MgData":
                    result = GetDefaultResult(httpContext, "MgData");
                    break;
                case "PData":
                    result = GetDefaultResult(httpContext, "PData");
                    break;
                case "StoragePredictions":
                    result = GetStorageResult(httpContext, "StoragePredictions");
                    break;

            }
            return result;
        }

        //获取贮存期预测曲线参数
        private AjaxResult GetStorageResult(HttpContext httpContext, string AssayType)
        {
            int RawID = Convert.ToInt32(httpContext.Request["RawID"]);
            int NormID = Convert.ToInt32(httpContext.Request["NormID"]);
            E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawID);

            List<E_StorageCalculateResult> list = tStorageCalculateResult.GetModelList("RawID=" + RawID + " and NormID=" + NormID + " order by Temperature asc");
            ArrayList DateList = new ArrayList();
            var XNamelist = new ArrayList();
            List<List<double>> TempListSum = new List<List<double>>();

            for (int i = -30; i <= 40; i++)
            {
                if (i % 5 == 0)
                {
                    XNamelist.Add(i);
                }
            }

            for (int i = 0; i < list.Count; i++)
            {
                List<double> TempList = new List<double>();
                E_StorageCalculateResult md = list[i];
                TempList.Add(Convert.ToDouble(md.Temperature));
                TempList.Add(Convert.ToDouble(md.LicunTime));
                TempListSum.Add(TempList);
            }

            var res = new ArrayList();
            res.Add(new
            {
                name = eRawMaterial.RAWName, //原料名称
                data = TempListSum //数据集合
            });

            var reauts = new { XName = XNamelist, YData = res };
            return new AjaxResult(true, reauts) { FullMsg = "What?" };
        }

        private AjaxResult GetDefaultResult(HttpContext httpContext, string AssayType)
        {
            DateTime startTime = Convert.ToDateTime(httpContext.Request["StartTime"]);
            DateTime endTime = Convert.ToDateTime(httpContext.Request["EndTime"]);
            string RawIDList = httpContext.Request["RawIDList"];
            string RawListName = httpContext.Request["RawListName"];

            ArrayList DateList = new ArrayList();
            var XNamelist = new ArrayList();

            for (DateTime time = startTime; time <= endTime; time = time.AddDays(1))
            {
                DateList.Add(time);
                XNamelist.Add(time.ToString("MM月dd日"));
            }

            var res = new ArrayList();

            for (int j = 0; j < RawIDList.Split(',').Length; j++)
            {
                double[] DataList = new double[DateList.Count];
                string RawID = RawIDList.Split(',')[j];
                for (int i = 0; i < DateList.Count; i++)
                {
                    double Score = 0;
                    E_FoodRecord eFoodRecord = new E_FoodRecord();
                    eFoodRecord.RawID = int.Parse(RawID);
                    eFoodRecord.FoodTime = Convert.ToDateTime(DateList[i]);
                    switch (AssayType)
                    {
                        case "SenseScore":
                            T_ScoreData tScoreData = new T_ScoreData();
                            E_ScoreData eScoreData = new E_ScoreData();
                            eScoreData.RawId = int.Parse(RawID);
                            eScoreData.ScoreTime = Convert.ToDateTime(DateList[i]);
                            List<E_ScoreData> ScoreList = tScoreData.GetDataEntityByTime(eScoreData);
                            Score = (ScoreList != null) ? ScoreList.Sum(p => p.Score) : 0;
                            break;
                        case "VCAssay":
                            eFoodRecord.FoodTypeID = 1; //检测
                            break;
                        case "WaterAssay":
                            eFoodRecord.FoodTypeID = 2; //水分含量
                            break;
                        case "ProteinAssay":
                            eFoodRecord.FoodTypeID = 4; //蛋白质含量
                            break;
                        case "MineralsAssay":
                            eFoodRecord.FoodTypeID = 5; //矿物质含量
                            break;
                        case "Carbohydrates":
                            eFoodRecord.FoodTypeID = 3; //碳水化合物
                            break;
                        case "VAAssay":
                            eFoodRecord.FoodTypeID = 6; //VA含量
                            break;
                        case "VEAssay":
                            eFoodRecord.FoodTypeID = 7; //VE含量
                            break;
                        case "AminoAcidData":
                            eFoodRecord.FoodTypeID = 8; //氨基酸含量
                            break;
                        case "DietaryFiberData":
                            eFoodRecord.FoodTypeID = 9; //膳食纤维含量
                            break;
                        case "CuData":
                             eFoodRecord.FoodTypeID = 10; //铜
                            break;
                        case "FeData":
                            eFoodRecord.FoodTypeID = 11; //铁
                            break;
                        case "ZnData":
                            eFoodRecord.FoodTypeID = 12; //锌
                            break;
                        case "CaData":
                            eFoodRecord.FoodTypeID = 13; //钙
                            break;
                        case "MgData":
                            eFoodRecord.FoodTypeID = 14; //镁
                            break;
                        case "PData":
                            eFoodRecord.FoodTypeID = 15; //镁
                            break;
                    }

                    if (AssayType != "SenseScore")
                    {
                        eFoodRecord = tFoodRecord.GetFoodRecord(eFoodRecord);
                        Score = (eFoodRecord != null) ? Convert.ToDouble(eFoodRecord.FoodResult) : 0;
                    }
                    DataList[i] = Score;
                }

                List<List<double>> TempListSum = new List<List<double>>();
                for (int k = 0; k < DataList.Count(); k++)
                {
                    List<double> TempList = new List<double>();
                    if (DataList[k] != 0)
                    {
                        TempList.Add(k);
                        TempList.Add(DataList[k]);
                        TempListSum.Add(TempList);
                    }
                }

                res.Add(new
                {
                    name = RawListName.Split(',')[j],
                    data = TempListSum
                });
            }
            var reauts = new { XName = XNamelist, YData = res };
            return new AjaxResult(true, reauts) { FullMsg = "What?" };
        }
    }
}